package com.example.demo.serviceImpl.remark;

import com.example.demo.dao.remark.RemarkDao;
import com.example.demo.dao.remark.RemarkWorkerDao;
import com.example.demo.dao.report.ReportDao;
import com.example.demo.dao.report.ReportRemarkDao;
import com.example.demo.po.remark.Remark;
import com.example.demo.service.remark.RemarkService;
import com.example.demo.service.report.ReportService;
import com.example.demo.service.user.UserService;
import com.example.demo.utils.Constant;
import com.example.demo.vo.ResultVO;
import com.example.demo.vo.remark.RemarkInfoVO;
import com.example.demo.vo.remark.RemarkVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class RemarkServiceImpl implements RemarkService {
    @Resource
    RemarkDao remarkDao;

    @Resource
    ReportRemarkDao reportRemarkDao;

    @Resource
    RemarkWorkerDao remarkWorkerDao;

    @Resource
    UserService userService;

    @Resource
    ReportService reportService;

    @Override
    public List<RemarkVO> getRemarkByRid(int rid,int pageNum) {
        List<RemarkVO> remarkVOList=new ArrayList<>();
        int pageSize=pageNum==-1?Constant.NO_PAGE_SIZE:Constant.DEFAULT_PAGE_SIZE;
        pageNum=pageNum==-1?1:pageNum;
        for(Remark remark:reportRemarkDao.selectRemarkByRid(rid,pageNum, pageSize)){
            remarkVOList.add(new RemarkVO(remark));
        }
        return remarkVOList;
    }

    @Override
    public ResultVO<RemarkInfoVO> submitRemark(RemarkInfoVO remarkInfoVO) {
        double active = userService.calculateActive(userService.getNameByUid(remarkInfoVO.getUid()));
        RemarkVO remarkVO=new RemarkVO(remarkInfoVO);
        int code=remarkDao.insertRemark(remarkVO);
        System.out.println(remarkVO.getReid());
        reportRemarkDao.insertReportRemark(remarkVO.getReid(),remarkInfoVO.getRid());
        remarkWorkerDao.insertRemarkWorker(remarkVO.getReid(),remarkInfoVO.getUid());
        reportService.updateAvgScore(remarkInfoVO.getRid(),remarkVO.getScore(),active);
        return new ResultVO<RemarkInfoVO>(code,"成功",new RemarkInfoVO(remarkVO));
    }
}
